var AudioManager = require('geoAudioManager');

cc.Class({
    extends: cc.Component,

    properties: {
        anim: {
            default: null,
            type: cc.Animation
        },
        hideAnimIndex:0,
        workingAnimIndex:1,
        sfx: {
            default: null,
            type: cc.AudioClip
        }
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
    },

    // update (dt) {},
    /**
     * 播放倒数音效，由帧事件触发
     */
    playSFX(){
        // 播放音效
        var id = AudioManager.Singleton.playSFX(this.sfx, false, 1);
        // var id = cc.audioEngine.playEffect(this.sfx, false);
        cc.log('ready counter audio id:' + id);
    },

    hide(){
        this._playAnim(this.hideAnimIndex);
    },

    doCount(complete){
        this._playAnim(this.workingAnimIndex, complete);
    },

    _playAnim(index, complete){
        if (index < 0)
            return;
        // 获取动画名称
        var clips = this.anim.getClips();
        var clip = clips[index];
        // 播放动画
        var animationState = this.anim.play(clip.name);
        if (complete) {
            // this.scheduleOnce(complete, animationState.duration + 0.1);
            this.anim.once(cc.Animation.EventType.FINISHED, complete);
        }
    }
});
